package com.moshang.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moshang.blog.entity.TbArticleInfo;
import com.moshang.blog.entity.TbMessage;
import com.moshang.blog.mapper.TbArticleInfoMapper;
import com.moshang.blog.mapper.TbMessageMapper;
import com.moshang.blog.service.TbMessageService;
import com.moshang.blog.core.utils.AddressUtils;
import com.moshang.blog.core.utils.IpUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @program: blog
 * @description: ${description}
 * @author: moshang
 * @create: 2018-12-21 17:05
 **/
@Service
@Transactional
public class TbMessageServiceImpl extends ServiceImpl<TbMessageMapper, TbMessage> implements TbMessageService {
@Resource
private TbArticleInfoMapper tbArticleInfoMapper;
    @Override
    public TbMessage saveOrUpdateMessage(TbMessage tbMessage, String insert,HttpServletRequest request) {
        String ip = IpUtils.getIpAddr();
        String address = AddressUtils.getRealAddressByIP(ip);
        tbMessage.setIp(ip);
        tbMessage.setAddress(address);
        tbMessage.setIsActivited(0);
        tbMessage.setCreateTime(new Date());
        if (tbMessage.getParentId()==null){
            tbMessage.setParentId(-1);
        }
        baseMapper.insert(tbMessage);
        //更新评论数
        if (tbMessage.getType()==1) {
            TbArticleInfo tbArticleInfo = tbArticleInfoMapper.selectById(tbMessage.getArticleId());
            if (tbArticleInfo != null) {
                tbArticleInfo.setComments(tbArticleInfo.getComments() + 1);
                tbArticleInfoMapper.updateById(tbArticleInfo);
            }
        }
        if (tbMessage.getParentId()!=-1) {
            TbMessage tbMessage1 = baseMapper.selectById(tbMessage.getParentId());
            List<TbMessage> list = new ArrayList<>();
            list.add(tbMessage);
            tbMessage1.setMessageList(list);
            return  tbMessage1;
        }else {
            return tbMessage;
        }


    }

    @Override
    public Integer findCount(Integer id) {
        QueryWrapper<TbMessage> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("article_id",id);
        queryWrapper.eq("type",1);
        Integer count=baseMapper.selectCount(queryWrapper);
        return count;
    }

    /**
     * 查询留言或评论总数
     * @param message
     * @return
     */

    @Override
    public Integer findMessage(Integer message) {
        QueryWrapper<TbMessage> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("is_activited",0);
        queryWrapper.eq("type",message);
        Integer count=baseMapper.selectCount(queryWrapper);
        return count;
    }

    @Override
    public List<TbMessage> selectMessages(List<TbMessage> list) {
        List<TbMessage> messageCount=new ArrayList<>();
            for (TbMessage tbMessage:list){
                messageCount.addAll(getChilds(tbMessage,list));
            }
        return messageCount;
    }
    public List<TbMessage> getChilds(TbMessage tbMessage ,List<TbMessage> list){
        List<TbMessage> messageCount=new ArrayList<>();
        QueryWrapper<TbMessage> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("is_activited",0);
        queryWrapper.eq("parent_id",tbMessage.getId());
        List<TbMessage> childs =baseMapper.selectList(queryWrapper);
        List<TbMessage> messageList=new ArrayList<>();
        if (childs.size()>0) {
            for (TbMessage tbMessage1 : childs) {
                messageList.add(tbMessage1);
                tbMessage.setMessageList(messageList);
                messageCount.add(tbMessage);
                getChilds(tbMessage1,childs);
            }
        }
        return  messageCount;
    }
}
